package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Author;
import com.ruoyi.system.service.IAuthorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作者管理Controller
 * 
 * @author 许雅萍
 * @date 2024-11-15
 */
@Controller
@RequestMapping("/system/author")
public class AuthorController extends BaseController
{
    private String prefix = "system/author";

    @Autowired
    private IAuthorService authorService;


    @GetMapping()
    public String author()
    {
        return prefix + "/author";
    }

    /**
     * 查询作者管理列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Author author)
    {
        startPage();
        List<Author> list = authorService.selectAuthorList(author);
        return getDataTable(list);
    }

    /**
     * 导出作者管理列表
     */
    @RequiresPermissions("system:author:export")
    @Log(title = "作者管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Author author)
    {
        List<Author> list = authorService.selectAuthorList(author);
        ExcelUtil<Author> util = new ExcelUtil<Author>(Author.class);
        return util.exportExcel(list, "作者管理数据");
    }

    /**
     * 新增作者管理
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }

    /**
     * 新增保存作者管理
     */
    @Log(title = "作者管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Author author)
    {
        return toAjax(authorService.insertAuthor(author));
    }

    /**
     * 修改作者管理
     */
    @GetMapping("/edit/{authorId}")
    public String edit(@PathVariable("authorId") Long authorId, ModelMap mmap)
    {
        Author author = authorService.selectAuthorByAuthorId(authorId);
        mmap.put("author", author);
        return prefix + "/edit";
    }

    /**
     * 修改保存作者管理
     */
    @Log(title = "作者管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Author author)
    {
        return toAjax(authorService.updateAuthor(author));
    }

    /**
     * 删除作者管理
     */
    @Log(title = "作者管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(authorService.deleteAuthorByAuthorIds(ids));
    }
}
